package queryProccessor;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class Update extends Command {

	public String[] getColumns() {
		return columns;
	}

	public void setColumns(String[] columns) {
		this.columns = columns;
	}

	public String[] getValues() {
		return values;
	}

	public void setValues(String[] values) {
		this.values = values;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	String[] columns;
	String[] values;
	String condition;
	private final String[] ideal1 = { "UPDATE", "table_name", "SET",
			"conditions", "WHERE", "condition" };
	private final String[] ideal2 = { "UPDATE", "table_name", "SET",
			"conditions" };
	String selectStatment;
	ArrayList<String> finalArray;
	boolean result = true;

	public Update(String s) {

		selectStatment = s;
		finalArray = new ArrayList<String>();
		convertToArray();
		result = checkSyntax();
	}

	public void conditions(String[] c) {
		columns = new String[c.length];
		values = new String[c.length];
		for (int i = 0; i < c.length; i++) {

			int index = search("=", c[i]);
			if (index != -1 && index != 0) {
				columns[i] = c[i].substring(0, index).trim();
				values[i] = c[i].substring(index + 1).trim();

			} else {
				result = false;
			}

		}

	}

	@Override
	public void convertToArray() {
		int up = search("UPDATE ", selectStatment);
		int set = search("SET ", selectStatment);
		int wh = search("WHERE ", selectStatment);
		int sc = search(";", selectStatment);
		if (up != -1 && set != -1 && sc != -1) {
			finalArray.add("UPDATE");
			String s = selectStatment.substring(up + 6, set);
			s = s.trim();
			finalArray.add(s);

			if (wh != -1) {
				finalArray.add("SET");
				s = selectStatment.substring(set + 3, wh);
				s = s.trim();
				finalArray.add(s);
				finalArray.add("WHERE");
				s = selectStatment.substring(wh + 5, sc);
				s = s.trim();
				finalArray.add(s);
			} else {
				finalArray.add("SET");
				s = selectStatment.substring(set + 3, sc);
				s = s.trim();
				finalArray.add(s);
			}
		}

	}

	public boolean lools(String[] ideal, int[] no) {
		boolean result = true;
		for (int i = 0; i < finalArray.size(); i++) {
			if (!isTrue(i, no)) {
				if (!finalArray.get(i).equalsIgnoreCase(ideal[i])) {
					result = false;
					break;
				}
			}
		}
		return result;
	}

	private boolean isTrue(int x, int[] no) {
		for (int i = 0; i < no.length; i++) {
			if (no[i] == x)
				return true;
		}
		return false;
	}

	public boolean checkSyntax() {
		boolean result = true;
		if (finalArray.size() == 6) {
			int[] no = { 1, 3, 5 };
			if (result == lools(ideal1, no)) {
				table_name = finalArray.get(1);
				String[] conditions = divide(finalArray.get(3));
				conditions(conditions);
				condition = finalArray.get(5);

			}

		} else if (finalArray.size() == 4) {
			int[] no = { 1, 3 };
			if (result == lools(ideal2, no)) {
				table_name = finalArray.get(1);
				String[] conditions = divide(finalArray.get(3));
				conditions(conditions);

			}

		}

		else
			result = false;
		return result;
	}

	private String[] divide(String s) {

		StringTokenizer str = new StringTokenizer(s, ",");
		String[] result = new String[str.countTokens()];
		int i = 0;
		while (str.hasMoreTokens()) {
			result[i] = str.nextToken().trim();
			i++;
		}
		return result;
	}

}
